﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-8-12
Description:jcxm三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class JcxmService
	{
		#region 增加一条数据
		public static int AddJcxm(Jcxm jcxm)
		{
			string sql =
			"INSERT INTO jcxm(title,content,sj,muban,hit) "+
			"VALUES (@title,@content,@sj,@muban,@hit)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@title",jcxm.Title),
						
					new SqlParameter("@content",jcxm.Content),
						
					new SqlParameter("@sj",jcxm.Sj),
						
					new SqlParameter("@muban",jcxm.Muban),
						
					new SqlParameter("@hit",jcxm.Hit)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteJcxmById(int id)	
		{
			string sql="DELETE "+"jcxm"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyJcxmByid(Jcxm jcxm)
		{
		string sql ="UPDATE "+"jcxm"+" set "+
					"title=@title, "+
					"content=@content, "+
					"sj=@sj, "+
					"muban=@muban, "+
					"hit=@hit "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",jcxm.Id),
					new SqlParameter("@title",jcxm.Title),
					new SqlParameter("@content",jcxm.Content),
					new SqlParameter("@sj",jcxm.Sj),
					new SqlParameter("@muban",jcxm.Muban),
					new SqlParameter("@hit",jcxm.Hit)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Jcxm> GetAllJcxms()
		{
			string sqlAll = "SELECT * FROM jcxm";
			return GetJcxmsBySql(sqlAll);
		}
		//以标识列获得对象
		public static Jcxm GetJcxmByid(object id)
		{
			string sql = "SELECT * FROM "+"jcxm"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Jcxm jcxm = new Jcxm();
					jcxm.Id=Convert.ToInt32(reader["id"]);
					jcxm.Title=Convert.ToString(reader["title"]);
					jcxm.Content=Convert.ToString(reader["content"]);
					jcxm.Sj=Convert.ToDateTime(reader["sj"]);
					jcxm.Muban=Convert.ToString(reader["muban"]);
					jcxm.Hit=Convert.ToInt32(reader["hit"]);
					reader.Close();
					
					return jcxm;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Jcxm> GetJcxmsBySql(string safeSql)
		{
			List<Jcxm> list = new List<Jcxm>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Jcxm jcxm = new Jcxm();
					jcxm.Id=Convert.ToInt32(row["id"]);
					jcxm.Title=Convert.ToString(row["title"]);
					jcxm.Content=Convert.ToString(row["content"]);
					jcxm.Sj=Convert.ToDateTime(row["sj"]);
					jcxm.Muban=Convert.ToString(row["muban"]);
					jcxm.Hit=Convert.ToInt32(row["hit"]);
					list.Add(jcxm);
				}
				return list;
			}
             
		}
		private static IList<Jcxm> GetJcxmsBySql( string sql, params SqlParameter[] values )
		{
			List<Jcxm> list = new List<Jcxm>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Jcxm jcxm = new Jcxm();
					jcxm.Id=Convert.ToInt32(row["id"]);
					jcxm.Title=Convert.ToString(row["title"]);
					jcxm.Content=Convert.ToString(row["content"]);
					jcxm.Sj=Convert.ToDateTime(row["sj"]);
					jcxm.Muban=Convert.ToString(row["muban"]);
					jcxm.Hit=Convert.ToInt32(row["hit"]);
					list.Add(jcxm);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<Jcxm> ShowjcxmForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from jcxm where  id not in ( select top "
						   +number+" id from jcxm order by id desc ) order by id desc";
			return GetJcxmsBySql(strsql);
		}
		#endregion
	}
}

